<?php
include_once ROOT . '/utils/Notifier.php';
$name = $_POST['name'];
$email = $_POST['email'];
$objet = $_POST['objet'];
$message = $_POST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$error = '';
if (empty($name) || empty($email)
    || empty($objet) || empty($message)
) {
    $error .= 'All fields must be filled<br/>';
}
if (preg_match("/^[a-zA-Z\s-]+$/i", $name) == 0) {
    $error .= 'Name must only contain characters<br/>';
}
if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
    $error .= 'Email is incorrect<br/>';
}
if (!empty($error)) {
    $data = array(
        'formAction' => getRoute('contactUsPost'),
        'nom' => $name,
        'email' => $email,
        'objet' => $objet,
        'message' => $message,
        'error' => $error
    );
    echo $template->render('contact/contactUs.php', $data);
    die();
}
$messageTosend = "<b>Name:</b> $name<br/>";
$messageTosend .= "<b>Ip:</b> $ip<br/><br/>";
$messageTosend .= $message;
$transport = Swift_SmtpTransport::newInstance(SMTP);
$mailer = Swift_Mailer::newInstance($transport);
$messageSwift = Swift_Message::newInstance($objet, $messageTosend, 'text/html', 'utf-8');
$messageSwift->addTo('arthurh.halet@gmail.com', 'Arthur Halet');
$messageSwift->addTo('bertrand.prouhet@free.fr', 'Bertrand Prouhet');
$messageSwift->addFrom($email);
$mailer->send($messageSwift);
$notification = new Notifier('Email sent', 'Your email has been sent');
echo $template->render('static/welcome.php', array('notifications' => array($notification)));
